package com.wolan.controller;

import com.wolan.entity.User;
import com.wolan.exception.BusinessExceptionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by fei.chen on 2016/3/24.
 */
@Controller
@RequestMapping(value="/user")
public class CommonController {
    @Autowired
    private BusinessExceptionFactory businessExceptionFactory;

    @RequestMapping(value="/getUser")
    public String index(){
        System.out.println("hello");
        return "index";
    }

    @RequestMapping(value="/getUser/{name}/{age}",method=RequestMethod.GET)
    public String index(@PathVariable String name,@PathVariable Integer age){
        System.out.println("name ="+name);
        System.out.println("age ="+age);
        return "index";
    }

    @RequestMapping(value="/form",method= RequestMethod.POST)
    public String form(@ModelAttribute("User") User user){
        System.out.println("name ="+user.getName());
        System.out.println("password ="+user.getPassword());
        return "index";
    }

    @RequestMapping(value="/getParameter",method= RequestMethod.GET)
    public String getParameter(HttpServletRequest request){
        System.out.println("name ="+request.getParameter("name"));
        return "index";
    }

    @RequestMapping(value="/requestParam",method= RequestMethod.GET)
    public String requestParam(@RequestParam(value="a",required = false) String str){
        System.out.println("str ="+str);
        return "index";
    }


    @RequestMapping(value="/json",method=RequestMethod.POST)
    public String form1(@RequestBody User user){
        if("1".equals(user.getId())){
            throw businessExceptionFactory.createBusinessException("10011");
        }
        System.out.println("id ="+user.getId());
        System.out.println("name ="+user.getName());
        System.out.println("password ="+user.getPassword());
        return "index";
    }

}
